
<template>
  <el-dialog v-model="dialogFormVisible" class="dialog-title" :title="title" :close-on-click-modal="false">
    <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="80px" style="margin-left:50px;">
      <el-form-item label="字典标签" prop="label">
        <el-input v-model="temp.label" />
      </el-form-item>
      <el-form-item label="字典值" prop="value">
        <el-input v-model="temp.value" />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="temp.sort" />
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="temp.remarks" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="Please input" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogFormVisible = false">取消</el-button>
      <el-button v-permission="['sys:dict:update']" type="primary" :loading="loading" @click="title==='新增'?createData():updateData()">
        确定
      </el-button>
    </template>
  </el-dialog>
</template>

<script>
import { createDict, updateDict } from '@/api/sys/dict/dict'
export default {
  name: 'MenuForm',
  emits: ['refreshList'],
  data() {
    return {
      dictGroup: {},
      temp: {
        id: undefined,
        label: '',
        value: '',
        sort: 1,
        remarks: ''
      },
      loading: false,
      dialogFormVisible: false,
      title: undefined,
      rules: {
        label: [{ required: true, message: '字典标签不能为空' }],
        value: [{ required: true, message: '字典值不能为空' }],
        sort: [{ required: true, message: '排序不能为空' }]
      }
    }
  },

  methods: {
    setdictGroup(row) {
      this.dictGroup = row
    },
    /**
     * 重置表单
     */
    resetTemp() {
      this.loading = false
      this.temp = {
        id: undefined,
        label: '',
        value: '',
        sort: 1,
        remarks: '',
        gid: this.dictGroup.id
      }
    },
    /**
     * 刷新列表
     */
    refreshList() {
      this.$emit('refreshList')
    },
    /**
     * 新增
     */
    handleCreate() {
      this.resetTemp()
      this.title = '新增'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs.dataForm.clearValidate()
      })
    },
    /**
     * 保存新增
     */
    createData() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.loading = true
          createDict(this.temp).then((response) => {
            this.loading = false
            const data = response.data
            if (data.success) {
              this.$store.dictStore.initDict()
              this.dialogFormVisible = false
              this.$message.success('创建成功')
              this.refreshList()
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    },
    /**
     * 编辑
     * @param row 选中数据
     */
    handleUpdate(row) {
      this.resetTemp()
      this.temp = Object.assign({}, row) // copy obj
      this.title = '编辑'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs.dataForm.clearValidate()
      })
    },
    /**
     * 保存编辑
     */
    updateData() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.loading = true
          const tempData = Object.assign({}, this.temp)
          updateDict(tempData).then((response) => {
            this.loading = false
            const data = response.data
            if (data.success) {
              this.$store.dictStore.initDict()
              this.dialogFormVisible = false
              this.$message.success(data.msg)
              this.refreshList()
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }

  }
}
</script>
<style scoped lang="scss">

</style>
